<?php

class CHomePageClass extends systemBase {

	private function getYears() {
		$lrYears = $this->Model->getYears();
		$this->Smarty->assign("years",$lrYears);
	}

	public function __construct($lsModuleName) {
		// check for loggin
		loginRequired(1);
		parent::__construct($lsModuleName);
	}

	public function actionDefault() {
		$this->Smarty->assign("ModuleName", "Zákazníci");
		$customers = $this->Model->getList(NVL(getGet("orderby"),"id"),NVL(getGet("order"),"asc"),getSession("year"));
		$this->Smarty->assign("customers", $customers);
		$this->getYears();
	}

	public function actionAdd() {
		$lsError = null;
		$lsShow = "form";
		//
		$this->Smarty->assign("ModuleName", "Přidat zákazníka");
		$lsForm = getPost("add");
		if(!empty($lsForm)) { // formular odeslan
			$lsJmeno    = mandatory(getPost("jmeno"), "Jméno", $lsError);
			$lsUlice    = mandatory(getPost("ulice"), "Ulice", $lsError);
			$lsTelefon  = getPost("telefon");
			$lsCas      = mandatory(getPost("cas"), "Čas", $lsError);
			$lnRok      = mandatory(getPost("rok"), "Rok", $lsError);
			$lsPoznamka = getPost("poznamka");
			//
			if(empty($lsError)) {
				$this->Model->addCustomer($lsJmeno, $lsUlice, $lsCas, $lnRok, $lsPoznamka, $lsTelefon);
				$lsShow = "added";
			}

		}
		// defaultni hodnoty
		$this->Smarty->assign("jmeno",NVL(getPost("jmeno"),""));
		$this->Smarty->assign("ulice",NVL(getPost("ulice"),""));
		$this->Smarty->assign("telefon",NVL(getPost("telefon"),""));
		$this->Smarty->assign("cas",NVL(getPost("cas"),""));
		$this->Smarty->assign("rok",NVL(getPost("rok"),DATE("Y")));
		$this->Smarty->assign("poznamka",NVL(getPost("poznamka"),""));
		//
		$this->Smarty->assign("error", $lsError);
		$this->Smarty->assign("show", $lsShow);
	}

	public function actionEdit() {
		$lsShow = "form";
		$lsError = null;
		//
		$this->Smarty->assign("ModuleName", "Upravit zákazníka");

		$lrCustomer = $this->Model->GetCustomer(getGet("id"));
		$this->Smarty->assign("jmeno",NVL(getPost("jmeno"),$lrCustomer["JMENO"]));
		$this->Smarty->assign("ulice",NVL(getPost("ulice"),$lrCustomer["ULICE"]));
		$this->Smarty->assign("telefon",NVL(getPost("telefon"),$lrCustomer["TELEFON"]));
		$this->Smarty->assign("cas",NVL(getPost("cas"),$lrCustomer["CAS"]));
		$this->Smarty->assign("rok",NVL(getPost("rok"),$lrCustomer["ROK"]));
		$this->Smarty->assign("poznamka",NVL(getPost("poznamka"),$lrCustomer["POZNAMKY"]));

		$lsForm = getPost("edit");
		if(!empty($lsForm)) { // formular odeslan
			$lsJmeno    = mandatory(getPost("jmeno"), "Jméno", $lsError);
			$lsUlice    = mandatory(getPost("ulice"), "Ulice", $lsError);
			$lsTelefon  = mandatory(getPost("telefon"), "Telefon", $lsError);
			$lsCas      = mandatory(getPost("cas"), "Čas", $lsError);
			$lnRok      = mandatory(getPost("rok"), "Rok", $lsError);
			$lsPoznamka = getPost("poznamka");
			//
			if(empty($lsError)) {
				$this->Model->editCustomer($lrCustomer["ID"],$lsJmeno, $lsUlice, $lsCas, $lnRok, $lsPoznamka, $lsTelefon);
				$lsShow = "editted";
			}
		} else {

		}
		$this->Smarty->assign("show",$lsShow);
		$this->Smarty->assign("error", $lsError);
	}

	public function actionDel() {
		$lsShow = "form";
		//
		$this->Smarty->assign("ModuleName", "Zákazníci");
		$lrCustomer = $this->Model->GetCustomer(getGet("id"));
		$this->Smarty->assign("jmeno",$lrCustomer["JMENO"]);
		$lsForm = getPost("delete");
		if(!empty($lsForm)) { // formular odeslan
			$this->Model->delCustomer($lrCustomer["ID"]);
			$lsShow = "deleted";
		}
		$this->Smarty->assign("show",$lsShow);
	}

	public function actionSetYear() {
		setSession("year",getGet("year"));
		redirect(getURL());
	}


}

?>
